#ifndef __EVENTLOOPTHREAD_H__
#define __EVENTLOOPTHREAD_H__
#include "EventLoop.h"
#include <atomic>
#include <thread>
#include <string>
#include <mutex>
#include <condition_variable>

using std::atomic_bool;
using std::thread;
using std::string;
using std::mutex;
using std::condition_variable;

class EventLoopThread
{
public:
    using ThreadInitCallback = std::function<void(EventLoop *)>;

    EventLoopThread(const ThreadInitCallback &cb = ThreadInitCallback(),
                    const string &name = string(""));
    ~EventLoopThread();
    EventLoop *startLoop(); //等待线程创建loop并返回所属EventLoop*

private:
    void threadFunc(); //创建loop

    EventLoop *_ownerLoop;        // 所属EventLoop
    atomic_bool _exiting;         // 正在退出
    thread _thread;               // 底层subReactor线程
    mutex _mutex;                 // 保护loop
    condition_variable _cond;     // 同步
    ThreadInitCallback _callback; // 线程初始化回调
    string _name;                 // 线程名
};

#endif
